package gsw.demo.pluggable.plugin_simple;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import gsw.demo.pluggable.R;

public class ActivityLogin extends Activity {
    EditText nameText;
    EditText passwordText;
    SharedPreferences share;
    //真实意图
    Intent oldIntent;
    private String name = "tom";
    private String password = "tom123";
    public static String OLD_INTENT = "oldIntent";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        nameText = (EditText) findViewById(R.id.name);
        passwordText = (EditText) findViewById(R.id.password);
        nameText.setText(name);
        passwordText.setText(password);
        share = this.getSharedPreferences("david", MODE_PRIVATE);//实例化
        oldIntent = getIntent().getParcelableExtra(OLD_INTENT);
        if (oldIntent != null) {
            ((TextView) findViewById(R.id.text)).setText(" 跳转界面：" + oldIntent.getComponent().getClassName());
        }
    }

    public void login(View view) {
        if ((nameText.getText() == null || passwordText.getText() == null)) {
            Toast.makeText(this, "请填写用户名 或密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (name.equals(nameText.getText().toString()) && password.equals(passwordText.getText().toString())) {
            SharedPreferences share = super.getSharedPreferences(HookManager.SPLOGIN, MODE_PRIVATE);
            SharedPreferences.Editor editor = share.edit(); //使处于可编辑状态
            editor.putString("nameText", nameText.getText().toString());
            editor.putString("sex", passwordText.getText().toString());
            editor.putBoolean(HookManager.SPLOGINSTATE, true);   //设置保存的数据
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            editor.commit();    //提交数据保存
            if (oldIntent != null) {
                startActivity(oldIntent);
                finish();
            }
        } else {
            SharedPreferences.Editor editor = share.edit(); //使处于可编辑状态
            editor.putBoolean("login", false);   //设置保存的数据
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
            editor.commit();    //提交数据保存
        }
    }
}
